package com.happystreet.utils
{
	import com.happystreet.GlobalGameConstants;
	import com.happystreet.config.GameConfig;

	public class DataUtils
	{
		private static const FLOOR_MARKED_AS_GARBAGE:String = "markedFloor";
		private static const ROOF_MARKED_AS_GARBAGE:String = "markedRoof";
		
		public function DataUtils()
		{
		}
		
		/**
		 *格式化对象成数组 
		 * @param data
		 * @return 
		 * 
		 */		
		public static function formatObjectDataToArray(data:Object):Array
		{
			var key:String;
			var array:Array;
			var list:Array = new Array();
			switch (typeof(data))
			{
				case "object":
					for (key in data)
					{
						array = [int(key), data[key]];
						list.push(array);
					}
					break;
			}
			return list;
		}
		/**
		 *核对街道数据
		 * @param value
		 * @return 
		 * 
		 */		
		public static function checkStreetData(value:Array):Array
		{
			var street:Object;
			for each (street in value)
			{
				street = checkShopLayout(street.content, street.level);
			}
			return value;
		}
		/**
		 *检测商店布局 
		 * @param content
		 * @param level
		 * @return 
		 * 
		 */		
		public static function checkShopLayout(content:Array, level:int):Array
		{
			var contentList:Array;
			var uniqueList:Array;
			var contentLen:int;
			var index_1:int;
			var index:int;
			var index_2:int;
			var layOut:int = GameConfig.getShopLayout(level)[1];
			var uniqueindex:int;
			for each (contentList in content)
			{
				if (contentList[0] == GlobalGameConstants.WALL_SHAPE_TYPE)
				{
					index++;
					if (index > layOut)
					{
						contentList[0] = FLOOR_MARKED_AS_GARBAGE;
						index_1++;
					}
				}
				if (contentList[0] == GlobalGameConstants.ROOF_TYPE)
				{
					index_2++;
					if (index_2 > 1)
					{
						contentList[0] = ROOF_MARKED_AS_GARBAGE;
						index_1++;
					}
				}
			}
			if (index_1 > 0)
			{
				trace("WARNING: Shop has " + index_1 + " illegal item(s), save may crash. Please contact your administrator to repair your shop.");
				contentLen = content.length - 1;
				while (contentLen >= 0)
				{
					switch (contentList[contentLen][0])
					{
						case FLOOR_MARKED_AS_GARBAGE:
							index--;
						case ROOF_MARKED_AS_GARBAGE:
							contentList.splice(contentLen, 1);
							break;
					}
					contentLen--;
				}
			}
			while (index < layOut)
			{
				contentList.unshift(GameConfig.getDefaultWallData());
				index++;
			}
			
			if (index_2 == 0)
			{
				content.splice(index, 0, GameConfig.getDefaultRoofData());
			}
			
			for each (uniqueList in content)
			{
				uniqueList.uniqueID = uniqueindex;
				uniqueindex++;
			}
			
			return content;
			
		}
		
		public static function getDoorsPosition(_arg1:Object):Array
		{
			var _local2:Array = new Array();
			if (!_arg1|| !_arg1.content)
			{
				return ([]);
			}
			var _local3:int;
			while (_local3 < _arg1.content.length)
			{
				if (_arg1.content[_local3][0] == GlobalGameConstants.DOOR_TYPE)
				{
					_local2.push([_local3, _arg1.content[_local3][2]]);
				}
				_local3++;
			}
			return (_local2);
		}
	}
}